package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.entity.realTime.EntityDistinctNewUserByHour;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 实时数据-小时新增用户manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class DistinctNewUserByHourManager extends ManagerQueryCacheBase<EntityDistinctNewUserByHour> {
	private static final long serialVersionUID = 1L;
	
	public List<EntityDistinctNewUserByHour> getChannelHourDistinctNewUserByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityDistinctNewUserByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid !=:channelid and serverid=:serverid order by hour desc ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	public List<EntityDistinctNewUserByHour> getHourDistinctNewUserByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityDistinctNewUserByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid order by hour ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}

    public List<EntityDistinctNewUserByHour> getHourDistinctNewUserByDateByTag(Date date, String queryPlatform, String queryChannel, String queryServer, String queryCountry) {
        return findByQuery("from EntityDistinctNewUserByHour where day>=? and day<? and platformid=? and channelid=? and serverid=? and tagvalue=? order by hour ", new Date(Time.getBeginningOfDay(date.getTime())), new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), queryPlatform, queryChannel, queryServer, queryCountry);
    }
	
	// 根据平台、渠道、区服更新小时新增用户数据
	public void updateHourDistinctNewUserByConditions(String updateType, Map<Integer, OverviewDetailData> map, Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer) {
		List<EntityDistinctNewUserByHour> ooLookDate = getHourDistinctNewUserByDate(lookDate, queryPlatform, queryChannel, queryServer);
		List<EntityDistinctNewUserByHour> ooCompareDate = getHourDistinctNewUserByDate(compareDate, queryPlatform, queryChannel, queryServer);
        List<EntityDistinctNewUserByHour> ooLastWeekDate = getHourDistinctNewUserByDate(lastWeekDate, queryPlatform, queryChannel, queryServer);
		if ("1".equals(updateType)) {  // 分时
            updateData(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        } else if ("2".equals(updateType)) {  // 累加
            updateData1(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        }
	}

    // 根据国家、区服更新小时新增用户数据
    public void updateHourDistinctNewUserByConditionsByTag(String updateType, Map<Integer, OverviewDetailData> map, Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer, String queryCountry) {
        List<EntityDistinctNewUserByHour> ooLookDate = getHourDistinctNewUserByDateByTag(lookDate, queryPlatform, queryChannel, queryServer, queryCountry);
        List<EntityDistinctNewUserByHour> ooCompareDate = getHourDistinctNewUserByDateByTag(compareDate, queryPlatform, queryChannel, queryServer, queryCountry);
        List<EntityDistinctNewUserByHour> ooLastWeekDate = getHourDistinctNewUserByDateByTag(lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry);
        if ("1".equals(updateType)) {  // 分时
            updateData(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        } else if ("2".equals(updateType)) {  // 累加
            updateData1(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        }
    }
	
	// 更新结果-分时
	private void updateData(Map<Integer, OverviewDetailData> map, List<EntityDistinctNewUserByHour> ooLookDate, List<EntityDistinctNewUserByHour> ooCompareDate, List<EntityDistinctNewUserByHour> ooLastWeekDate) {
		for (EntityDistinctNewUserByHour o : ooLookDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setDistinctNewUserLookValue(o.getNew_user() + odd.getNewUserLookValue());
			}
		}
		
		for (EntityDistinctNewUserByHour o : ooCompareDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setDistinctNewUserCompareValue(o.getNew_user() + odd.getNewUserCompareValue());
			}
		}

        for (EntityDistinctNewUserByHour o : ooLastWeekDate) {
            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                odd.setDistinctNewUserLastWeekValue(o.getNew_user() + odd.getNewUserLastWeekValue());
            }
        }
	}

    // 更新结果-累计
    private void updateData1(Map<Integer, OverviewDetailData> map, List<EntityDistinctNewUserByHour> ooLookDate, List<EntityDistinctNewUserByHour> ooCompareDate, List<EntityDistinctNewUserByHour> ooLastWeekDate) {
        int nowAdd1 = 0;
        int index1 = 0;
        for (EntityDistinctNewUserByHour o : ooLookDate) {
            if(o.getHour() > index1++) {
                for (int i = index1 - 1; i < o.getHour(); i++) {
                    OverviewDetailData odd = map.get(i);
                    if (odd != null) {
                        odd.setDistinctNewUserLookValue(nowAdd1);
                    }
                }
            }

            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                nowAdd1 += o.getNew_user() + odd.getNewUserLookValue();
                odd.setDistinctNewUserLookValue(nowAdd1);
            }
        }

        int nowAdd2 = 0;
        int index2 = 0;
        for (EntityDistinctNewUserByHour o : ooCompareDate) {
            if(o.getHour() > index2++) {
                for (int i = index2 - 1; i < o.getHour(); i++) {
                    OverviewDetailData odd = map.get(i);
                    if (odd != null) {
                        odd.setDistinctNewUserCompareValue(nowAdd2);
                    }
                }
            }

            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                nowAdd2 += o.getNew_user() + odd.getNewUserCompareValue();
                odd.setDistinctNewUserCompareValue(nowAdd2);
            }
        }

        int nowAdd3 = 0;
        int index3 = 0;
        for (EntityDistinctNewUserByHour o : ooLastWeekDate) {
            if(o.getHour() > index3++) {
                for (int i = index3 - 1; i < o.getHour(); i++) {
                    OverviewDetailData odd = map.get(i);
                    if (odd != null) {
                        odd.setDistinctNewUserLastWeekValue(nowAdd3);
                    }
                }
            }

            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                nowAdd3 += o.getNew_user() + odd.getNewUserLastWeekValue();
                odd.setDistinctNewUserLastWeekValue(nowAdd3);
            }
        }
    }
}
